<template>
  <el-container>
    <el-header>
      <h3>学员练车记录</h3>
    </el-header>
    <el-main>
      <el-form :inline="true" ref="form" :model="form">
          <el-form-item label="日期选择" prop="select_time">
            <el-date-picker type="date" v-model="form.select_time" format="yyyy-MM-dd" value-format="yyyy-MM-dd" placeholder="请选择时间"></el-date-picker>
          </el-form-item>
        <el-form-item label="时间段选择" prop="practice_time">
            <el-select v-model="form.practice_time">
                <el-option
                  v-for="item in timeLimit"
                  :label="item.time_limit"
                  :key="item.id"
                  :value="item.id"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="练车科目" prop="subject">
          <el-select v-model="form.subject">
            <el-option
              v-for="item in subjectList"
              :label="item.name"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="练车状态" prop="practice_status">
          <el-select v-model="form.practice_status">
            <el-option
              v-for="item in statusList"
              :label="item.name"
              :key="item.id"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click.prevent="search">查询</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click.prevent="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        border
        v-loading="loading"
        :cell-style="changeStyle"
      >
        <el-table-column label="图像">
          <template slot-scope="scope">
            <el-avatar :src="scope.row.avatar" :size="50"></el-avatar>
          </template>
        </el-table-column>
        <el-table-column
          label="姓名"
          prop="name"
        >
        </el-table-column>
        <el-table-column label="手机号"
          prop="mobile"
        >
        </el-table-column>
        <el-table-column
          label="日期"
          prop="date"
        ></el-table-column>
        <el-table-column
          label="时间段"
          prop="time"
        ></el-table-column>
        <el-table-column
          label="科目"
          prop="subject"
        ></el-table-column>
        <el-table-column
          label="状态"
          prop="status"
        ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link v-if="scope.row.status == '进行中'" @click="cancelReserve(scope.$index)">取消</el-link>
            <el-link v-else disabled>无法操作</el-link>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="page"
        :page-size="pageSize"
        @current-change="changePage"
      >
      </el-pagination>
    </el-footer>
  </el-container>
</template>

<script>
  import {getTimeLimit, reserveList, cancelReserve} from '@/api/student'
    export default {
        name: "record",
        inject: ['reload'],
        data () {
            return {
                form: {
                    select_time: '',
                    practice_time: 0,
                    subject: 0,
                    practice_status: 0
                },
                student_id: 0,
                timeLimit: [
                    {id: 0, time_limit: '全部'}
                ],
                subjectList: [
                    {id: 0 , name: '全部'},
                    {id: 1,name: '科目二'},
                    {id: 2, name: '科目三'}
                ],
                statusList: [
                    {id: 0, name: '全部'},
                    {id: 1, name: '进行中'},
                    {id: 2, name: '已完成'},
                    {id: 3, name: '已取消'},
                    {id: 4, name: '未练车'},
                ],
                tableData:[],
                page: 1,
                pageSize: 10,
                total: 1,
                loading : true,
            };
        },
        methods: {
          search() {
            var params = this.form;
            params.student_id = this.student_id;
            this.loading = true;
            reserveList(params).then(response => {
                this.tableData = response.data.list;
                this.page = response.data.pagination.current;
                this.pageSize = response.data.pagination.pageSize;
                this.total = response.data.pagination.total;
                this.loading = false;
            }).catch( err => {
                this.loading = false;
            })
          },

          reset(ref) {
            this.$refs[ref].resetFields();
          },

          changePage(currentPage) {
              var params = this.form;
              params.student_id = this.student_id;
              params.page = currentPage;
              params.pageSize = this.pageSize;
              this.loading = true;
              reserveList(params).then(response => {
                  this.tableData = response.data.list;
                  this.page = Number(response.data.pagination.current);
                  this.pageSize = Number(response.data.pagination.pageSize);
                  this.total = Number(response.data.pagination.total);
                  this.loading = false;
              }).catch( err => {
                  this.loading = false;
              })
          },
          cancelReserve (index) {
            var params = {
                student_id: this.student_id,
                reserve_record_id: this.tableData[index].id
            };
            this.loading = true;
            cancelReserve(params).then(response => {
              this.$message.success(response.msg);
              this.loading = false;
              this.reload();
            }).catch( err => {
                this.loading = false;
            })
          },
          generateTime () {
              var now = new Date();
              var time = now.getFullYear() + "-" +((now.getMonth()+1)<10?"0":"")+(now.getMonth()+1)+"-"+(now.getDate()<10?"0":"")+now.getDate();
              return time;
          },
          changeStyle (row, column, rowIndex, columnIndex) {
            //这里主要是修改状态那一列的值 颜色
            if (row.column.label == '状态') {
                if (row.row.status == '进行中') {
                    return 'color: blue';
                } else if (row.row.status == '已取消') {
                    return 'color: red';
                }  else if (row.row.status == '未完成') {
                    return 'color: yellow';
                } else if (row.row.status == '已完成') {
                    return 'color: green';
                }
            }
          }
        },
        mounted() {
            this.student_id = this.$route.query.student_id;
            getTimeLimit({student_id: this.student_id}).then(response => {
                response.data.forEach( row => {
                    this.timeLimit.push({id: row.id, time_limit: row.time_limit});
                });

                reserveList({student_id: this.student_id}).then(response => {
                    this.loading = false;
                    this.tableData = response.data.list;
                    this.page = response.data.pagination.current;
                    this.pageSize = response.data.pagination.pageSize;
                    this.total = response.data.pagination.total;
                })
            }).catch(err => {
                this.loading = false;
            })
        }
    }
</script>

<style scoped>
  .el-container{
    width: 99%;
    max-width: 100%;
    overflow: hidden;
    position: relative;
    margin: 10px auto;
    border: 1px solid #F2F6FC;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  }

  .el-header{
    width: 100%;
    text-align: center;
  }

  .el-header h3 {
    font-size: 20px;
    height: 20px;
    line-height: 20px;
  }

  .el-footer {
    text-align: center;
  }
</style>
